package ssm.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;

@Controller
public class RedisController {
    @Autowired
    private JedisCluster jedisCluster;
    @Autowired
    private Jedis jedis;

    /*若有密码则在操作前加 jedis.auth("123456"); */

    //集群版Redis  [...create --replicas 1 IP:Port*6](三主各带一从)
    //添加普通字符串<K,V>
    @RequestMapping(value="redis/set",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject set(@RequestParam("key")String key,@RequestParam("value")String value){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="键值对<"+key+","+value+">添加(/覆写)失败";
        String Result=jedisCluster.set(key,value);
        if(Result.equals("OK")){
            statusCode=1;
            msg="键值对<"+key+","+value+">添加(/覆写)成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //获取普通字符串<K,V>
    @RequestMapping(value="redis/get",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject get(@RequestParam("key")String key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="该key="+key+"不存在";
        boolean flag=jedisCluster.exists(key);
        if(flag){
            statusCode=1;
            String value=jedisCluster.get(key);
            msg="获取到:<"+key+","+value+">";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //删除普通字符串<K,V>
    @RequestMapping(value="redis/del",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject del(@RequestParam("key")String key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="del-Key操作失败";
        Long AffectLine=jedisCluster.del(key);
        if(AffectLine>0){
            statusCode=1;
            msg="key("+key+")已成功删除";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //哈希、List、Set、散列、有序集合暂时不写

/*-------------------------------------------- 分割线 --------------------------------------------------------------------------------------------------------*/
    //单机版Redis,远程：腾讯云服务器
    //添加普通字符串<K,V>  可以添加[整数][浮点数据][字符串]--》如: "3"，"5.2"，"abc"
    @RequestMapping(value="redis/rset",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject Rset(@RequestParam("key")String key,@RequestParam("value")String value){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="键值对<"+key+","+value+">添加(/覆写)失败";
        String Result=jedis.set(key,value);
        if(Result.equals("OK")){
            statusCode=1;
            msg="键值对<"+key+","+value+">添加(/覆写)成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //获取普通字符串<K,V>
    @RequestMapping(value="redis/rget",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject Rget(@RequestParam("key")String key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="该key="+key+"不存在";
        boolean flag=jedis.exists(key);
        if(flag){
            statusCode=1;
            String value=jedis.get(key);
            msg="获取到:<"+key+","+value+">";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //删除普通字符串<K,V>
    @RequestMapping(value="redis/rdel",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject Rdel(@RequestParam("key")String key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="del-Key操作失败";
        Long AffectLine=jedis.del(key);
        if(AffectLine>0){
            statusCode=1;
            msg="key("+key+")已成功删除";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //清空数据
    @RequestMapping(value="redis/rflushDB",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RFlushDB(){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="redis清空数据失败";
        String result=jedis.flushDB();
        if(result.equals("OK")){
            statusCode=1;
            msg="redis清空数据成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //设置键的生存时间(单位：seconds)
    @RequestMapping(value="redis/r-expire",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RExpire(@RequestParam("key")String key,@RequestParam("lifetime")Integer lifetime){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="redis设置生存时间失败";
        Long result=jedis.expire(key,lifetime);
        if(result==1||result>0){
            statusCode=1;
            msg="redis设置生存时间"+lifetime+"秒: 成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //查看键的剩余生存时间[>0:在生存计时中][=-1:永久生存][=-2:已死亡,即该key不存在]
    @RequestMapping(value="redis/r-ttl",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RCheckTTL(@RequestParam("key")String key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="不存在的key";
        boolean isExist=jedis.exists(key);
        if(isExist){
            statusCode=1;
            Long result=jedis.ttl(key);
            if(result==-1) msg="Key剩余生命时间：永久";
            if(result>0) msg="Key剩余生命时间："+result+"秒";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //查看键值类型[Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)]
    @RequestMapping(value="redis/r-type",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RTypeof(@RequestParam("key")String key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="不存在的key";
        boolean isExist=jedis.exists(key);
        if(isExist) {
            statusCode=1;
            String result = jedis.type(key);
            msg=key+"的类型为："+result;
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //字符串append
    @RequestMapping(value="redis/r-append",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RAppend(@RequestParam("key")String key,@RequestParam("append")String append) {
        JSONObject json = new JSONObject();
        int statusCode = -1;
        String msg = "尾部拼接字符失败";
        Long result = jedis.append(key, append);
        if (result > 0) {
            statusCode = 1;
            msg = "尾部拼接字符成功,字符串总长度为(汉字占三位)：" + result;
        }
        json.put("statusCode", statusCode);
        json.put("msg", msg);
        return json;
    }

    //增加多个键值对
    @RequestMapping(value="redis/r-mset",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RMset(HttpServletRequest request,@RequestParam("key-value") String... keyvalue){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="多键值对添加失败";
        String[] KV=new String[keyvalue.length*2];//{key1,value1,key2,value2...}
        if(keyvalue!=null&&keyvalue.length>0){
            for(int i=0;i<keyvalue.length;i++){
                KV[i*2]=keyvalue[i].split("@amp@")[0];
                KV[i*2+1]=keyvalue[i].split("@amp@")[1];
            }
            jedis.mset(KV);
            statusCode=1;
            msg="多键值对添加成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //获取多个键值对
    @RequestMapping(value="redis/r-mget",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RMget(HttpServletRequest request,@RequestParam("key") String... key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="多键值对Get失败";
        String[] KV={};//{key1,value1,key2,value2...}
        if(key!=null&&key.length>0){
            //jedis.mget(key);为了方便看到每个键的结果，该方法替换如下
            for(String k:key) json.put(k,jedis.get(k));
            statusCode=1;
            msg="多键值对Get成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //删除多个键值对
    @RequestMapping(value="redis/r-mdel",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RMdel(HttpServletRequest request,@RequestParam("key") String... key){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="多键值对Del失败";
        String[] KV={};//{key1,value1,key2,value2...}
        if(key!=null&&key.length>0){
            statusCode = 1;
            Long AffectLine=jedis.del(key);
            if(AffectLine>0&&AffectLine==key.length) {
                msg = "多键值对(所有请求Key)Del成功";
            }else if(AffectLine>0&&AffectLine<key.length){
                msg = "多键值对(部分请求Key)Del成功";
            }
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //根据特征遍历当前redis中的key
    @RequestMapping(value="redis/r-ergodic",method= RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject RErgodic(@RequestParam("pattern") String pattern){
        JSONObject json=new JSONObject();
        int statusCode=-1;
        String msg="获取Key列表失败";
        Set<String> set=jedis.keys(pattern);
        if(set!=null&&set.size()>0){
            statusCode = 1;
            JSONArray ja=new JSONArray();
            for(String keyname:set)
                ja.add(keyname);
            json.put("keyList",ja);
            msg = "获取Key列表成功";
        }
        json.put("statusCode",statusCode);
        json.put("msg",msg);
        return json;
    }

    //列表
    //
    //

    //Set集合
    //
    //

    //散列
    //
    //

    //Hash
    //
    //

}
